package com.example.plantuml.aspect;

import com.example.plantuml.model.RequestTrace;
import com.example.plantuml.tools.CallChainManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 请求跟踪AOP切面，用于无侵入式地记录方法调用链
 */
public class BaseRequestTraceAspect {

    @Autowired
    protected CallChainManager callChainManager;

    protected Object around(ProceedingJoinPoint joinPoint, HttpServletRequest request) throws Throwable {
        if (!Objects.isNull(request)) {
            RequestTrace requestTrace = new RequestTrace(request);
            //启动trace
            callChainManager.startRequestTrace(requestTrace);
        }
        callChainManager.startMethodCall(joinPoint);
        try {
            // 执行目标方法
            return joinPoint.proceed();
        } finally {
            // 结束方法调用跟踪
            callChainManager.endMethodCall();
        }

    }


}
